package de.ohmhochschule.bme;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

public class TicTacToeActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // add new view and connect it to button via event handler:
        View aboutButton = this.findViewById(R.id.btn_about);
        aboutButton.setOnClickListener(this);
        View exitButton = this.findViewById(R.id.btn_exit);
        exitButton.setOnClickListener(this);
        View newGameButton = this.findViewById(R.id.btn_new_game);
        newGameButton.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		switch ( v.getId() ) {
		case R.id.btn_about:
			Intent i = new Intent(this, AboutActivity.class);	
			startActivity(i);
			break;
		case R.id.btn_exit:
			finish();
			break;
		case R.id.btn_new_game:
			openNewGameDialog();
			break;
		default:
			Log.e(getLocalClassName(), "We should never get here!");			
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu( menu );
		MenuInflater inflater = getMenuInflater();
		inflater.inflate( R.menu.menu, menu );
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch ( item.getItemId() ) {
		case R.id.settings:
			startActivity( new Intent( this, SettingsActivity.class ) );
			return true;
		}
		return false;
	}
	
	private void openNewGameDialog() {
		new AlertDialog.Builder(this)
			.setTitle(R.string.new_game_title)
			.setItems(R.array.who_starts, new DialogInterface.OnClickListener() {				
					@Override
					public void onClick(DialogInterface dialog, int which) {
					      Intent intent = new Intent( TicTacToeActivity.this, GameActivity.class );
					      intent.putExtra( GameActivity.STARTING_PLAYER, which );
					      startActivity( intent );
					}
				})
			.show();
	}
}